<template>
  <MyDrawer ref="myRef" title="楼盘特色" :disabled="false" @submit="submit">
    <el-form :model="drawerProps.row" :rules="rules" ref="formRef" label-width="120">
      <el-form-item label="特色名称" prop="name">
        <el-input v-model="drawerProps.row.name"></el-input>
      </el-form-item>
      <el-form-item label="排序值">
        <el-input-number v-model="drawerProps.row.sort" :min="1" :max="100"></el-input-number>
      </el-form-item>
      <el-form-item label="是否显示">
        <el-switch
          :active-value="true"
          :inactive-value="false"
          active-text="是"
          inactive-text="否"
          v-model="drawerProps.row.showInSearch"
        ></el-switch>
      </el-form-item>
    </el-form>
  </MyDrawer>
</template>

<script setup lang="ts">
import MyDrawer from "@/components/MyDrawer/index.vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { ref } from "vue";

const myRef = ref<InstanceType<typeof MyDrawer> | null>();

interface DrawerProps {
  row: any;
  api?: (params: any) => Promise<any>;
  getTable?: () => Promise<any>;
}

const rules: FormRules = {
  name: [
    { required: true, message: "特色名称不可为空", trigger: "blur" },
    { max: 10, min: 1, message: "字数在1-10个字之间" }
  ]
};

const initData: any = {
  name: "",
  sort: 1,
  showInSearch: true
};

const drawerProps = ref<DrawerProps>(JSON.parse(JSON.stringify(initData)));

const acceptParams = (params: any) => {
  drawerProps.value = params;
  myRef.value?.acceptParams();
};

const formRef = ref<FormInstance>();

const submit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success("成功");
      await drawerProps.value.getTable!();
      myRef.value?.close();
    } catch (error) {}
  });
};

defineExpose({ acceptParams });
</script>

<style></style>
